#include "engine.h"

#include "../game/game.h"

#include "point.h"
#include "config.h"
#include <stdlib.h>
#include <list>

using namespace global;

Direction CAI_Engine::MakeDesision()
{
	CellType** visibleMap = m_ptrAgent->GetVisibleMap();
	Point agentPosition = m_ptrAgent->GetPosition();

	std::list<Direction> positionRequests;
	std::map<int, Direction> values;
	
	positionRequests.push_back(D_LEFT);
	positionRequests.push_back(D_RIGHT);
	positionRequests.push_back(D_UP);
	positionRequests.push_back(D_DOWN);

	for(auto it = positionRequests.cbegin(); it != positionRequests.cend(); ++it)
	{
		int rate = m_ptrAgent->CalculatePosition((*it));
		switch (rate)
		{
			case A_CANNOTMOVE:
				randomDirection = (Direction)(1+int(4*rand()/(RAND_MAX + 1.0)));
				break;

			case A_ISINHISTORY:
				rate *= rate;
				break;

			case A_NOTHING:
				while(!m_ptrAgent->ReadyToGo(randomDirection))
				{
					randomDirection = (Direction)(1+int(4*rand()/(RAND_MAX + 1.0)));
				}
				return randomDirection;
				break;

			default:
				randomDirection = (Direction)(1+int(4*rand()/(RAND_MAX + 1.0)));
		}
		
		values.insert(std::make_pair(rate, (*it)));
	}
	
	return values.begin()->second;
}